package com.git.dengchongshuang.managementsystem;

import com.git.dengchongshuang.managementsystem.exception.LoginException;

import java.util.ArrayList;
import java.util.List;

public class Login {
    private final static List<Account> accounts = new ArrayList<>();//存储所有用户
    static {
        Account account = new Account();
        account.setUsername("邓崇雙");
        account.setPassword("123456");
        accounts.add(account);
    }
    public static boolean login(String username,String password) throws LoginException {
        if(accounts.isEmpty()) {
            throw new LoginException("用户名不存在!");
        }
        else {
            for(Account account:accounts) {
                if(account.getUsername().equals(username) && account.getPassword().equals(password)) {
                    return true;
                }
            }
            return false;
        }
    }
    public static boolean  register(String username,String password) throws LoginException {
        if(accounts.isEmpty()) {
            AddAccount(username,password);
            return true;
        }
        else {
            for(Account account:accounts) {
                if(account.getUsername().equals(username)) {
                    throw new LoginException("用户名已存在!");
                }
            }
            AddAccount(username,password);
            return false;
        }
    }
//    添加用户方法
    private static void AddAccount(String username,String password) {
        Account account = new Account();
        account.setUsername(username);
        account.setPassword(password);
        accounts.add(account);
    }
}
